#!/bin/bash

# 配置参数
APP_ID="AAAAAkey"
APP_SECRET="BBBBBSecret"
API_URL="http://localhost:8080/api/token"

# 生成当前时间戳
TIMESTAMP=$(date +%s%3N)

# 计算MD5哈希值（大写）
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS
    AUTHORIZATION=$(echo -n "${APP_SECRET}${TIMESTAMP}" | md5 | tr '[:lower:]' '[:upper:]')
else
    # Linux
    AUTHORIZATION=$(echo -n "${APP_SECRET}${TIMESTAMP}" | md5sum | cut -d ' ' -f1 | tr '[:lower:]' '[:upper:]')
fi

echo "使用的参数:"
echo "APP_ID: $APP_ID"
echo "APP_SECRET: $APP_SECRET"
echo "时间戳: $TIMESTAMP"
echo "计算的Authorization值: $AUTHORIZATION"
echo ""

# 构建URL
URL="${API_URL}?t=${TIMESTAMP}"

# 发送请求获取token
echo "发送请求到: $URL"
echo "请求头:"
echo "Authorization: $AUTHORIZATION"
echo "Accesskey: $APP_ID"
echo ""

response=$(curl -s -X GET "$URL" \
    -H "Content-Type: application/json" \
    -H "Authorization: $AUTHORIZATION" \
    -H "Accesskey: $APP_ID")

echo "响应结果:"
echo "$response" | python -m json.tool 2>/dev/null || echo "$response"

# 提取token（如果响应是JSON格式）
token=$(echo "$response" | grep -o '"token":"[^"]*' | cut -d'"' -f4)
if [ ! -z "$token" ]; then
    echo ""
    echo "获取的JWT令牌:"
    echo "$token"
fi 